<?php
class account_model extends Mymodel {
	public $table_name = "patient_history";
	public $table_name2 = "list_price";
	public $table_name3 ="patient_checkups";
        function Account_model() {
		parent::Mymodel ();
	}
	
	function get_item_price($id = NULL) {
		$item_data = $this->get_data ( $this->table_name2, $id );
		//print_r($item_data);
		if(!empty($item_data['price']))
		return $item_data ['price'];
		return 0;
	}
	function get_patient_checkups($patient_id = NULL,$condition=array()) {
		$id = NULL;
		
		if ($patient_id !== NULL)	$condition[$this->table_name.'.patient_id'] = $patient_id ;
		
		return $this->get_data_joined ( $this->table_name, $this->table_name3,'patient_history.id=patient_checkups.preview_id and patient_history.patient_id=patient_checkups.patient_id  ', NULL, $condition , 'preview_date');
	
	}
	
	function get_chceckups_total($patient_id=NULL,$month=NULL,$year=NULL){
		if($month==NULL && $year==NULL){ 
			
			$condition=array('day(preview_date)'=>date('d'),'month(preview_date)'=>date('m'),'year(preview_date)'=>date('Y'));
		}
		elseif($month!==NULL && $year!=NULL){
			$condition=array('month(preview_date)'=>$month,'year(preview_date)'=>$year);
		}
		elseif($month==NULL && $year!=NULL){
			$condition=array('year(preview_date)'=>$year);
		}else $condition=array();
		
		$patient_checkups = $this->get_patient_checkups ($patient_id,$condition );
		//print_r($patient_checkups);
		//$this->show_the_query();
        $total=0;
		foreach ( $patient_checkups  as $row ) {
			//print_r($row);
			 if(empty($row ['checkup_id'])) continue;
			 $checkup = $row ['checkup_id'];
			 
		         $total+=$this->get_item_price ( $checkup );	
			
		}
		
		#print_r($patient_checkups);
		echo $total;
		return $total;
		
	}
	function get_previews_cost($patient_id,$condition=array()) {
		
		$patient_checkups = $this->get_patient_checkups ( $patient_id,$condition );
		$data = array ();
        $data['count']=0;
		foreach ( $patient_checkups  as $row ) {
			 
			 $checkup = $row ['checkup_id'];
			 if(empty($checkup)) continue;
			$data['count']++;
			
			//foreach ( $checkup_ids as $checkup ) {
				$data[$row['patient_id']] [$row ['preview_id']] ['checkup'] [$checkup] = $this->get_item_price ( $checkup );
				$data[$row['patient_id']] [$row ['preview_id']] ['preview_date'] = $row ['preview_date'];
			//}
			
			
		}
		if(!empty($data[$patient_id]))
		  $data['count']=count($data[$patient_id]);
		#print_r($patient_checkups);
		return $data;
	}

}
